<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" v-model="chekedAll"> 全选
                    </th>
                    <th>车名</th>
                    <th>价格</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in goodsList" :key="item.id">
                    <td>
                        <input type="checkbox" :value="item" v-model="checkedArr">
                    </td>
                    <td>{{item.name}}</td>
                    <td>{{item.price}}</td>
                </tr>
            </tbody>
        </table>

    </div>
</body>

</html>
<script>
    //  使用计算属性实现全选反选的效果

    const app = Vue.createApp({
        data() {
            return {
                goodsList: [
                    {
                        id: 1,
                        name: "马自达-mx-5",
                        price: '40W'
                    },
                    {
                        id: 2,
                        name: "奔驰glc",
                        price: '45w'
                    },
                    {
                        id: 3,
                        name: "五菱宏光",
                        price: '4.5w'
                    }
                ],
                checkedArr: [], // 选中的项组成的数组
            }
        },
        methods: {

        },
        computed: {
            chekedAll: {
                get() {
                    // 
                    return this.checkedArr.length == this.goodsList.length
                },
                set(val) {
                    // console.log(val);
                    if (val) {
                        this.checkedArr = this.goodsList
                    } else {
                        this.checkedArr = []
                    }
                }
            }
        }
    }).mount('#app')

</script>